﻿Public Class ctrThanhToan

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
    End Sub

    Private Sub ctrThanhToan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvBan_Load()
        gvChiTietHD_Load()
        TongTien_Load()
    End Sub

    Private Sub gvBan_Load()
        Dim bus As GeneralBUS = New GeneralBUS()
        gvBan.DataSource = bus.GetTable("select * from BAN where TinhTrang='Đầy'")
    End Sub

    Private Sub gvChiTietHD_Load()
        Dim bus As GeneralBUS = New GeneralBUS()
        gvChiTietHD.DataSource = bus.GetTable("select CT.MaHD, TenMon, SoLuong, ThanhTien" _
                                              & " from CHITIETHOADON CT, HOADON HD, THUCDON TD" _
                                              & " where TD.MaMon=CT.MaMon and CT.MaHD=HD.MaHD and MaBan=" + gvBan.CurrentRow.Cells(0).Value.ToString())
    End Sub

    Private Sub gvBan_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvBan.SelectionChanged
        gvChiTietHD_Load()
        TongTien_Load()
    End Sub

    Private Sub TongTien_Load()
        Dim bus As GeneralBUS = New GeneralBUS()
        lbTongTien.Text = bus.GetValue("select sum(ThanhTien)" _
                                       & " from CHITIETHOADON CT, HOADON HD, THUCDON TD" _
                                       & " where TD.MaMon=CT.MaMon and CT.MaHD=HD.MaHD and MaBan=" + gvBan.CurrentRow.Cells(0).Value.ToString())
    End Sub

    Private Sub btnThanhToan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThanhToan.Click
        Dim tmp As BanDTO = New BanDTO()
        tmp.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()
        tmp.TinhTrang = "Trống"

        Dim tmp2 As HoaDonDTO = New HoaDonDTO()
        tmp2.TinhTrangTT = True
        tmp2.MaHD = gvChiTietHD.CurrentRow.Cells(0).Value.ToString()
        tmp2.ThoiGian = Date.Now.ToString()
        tmp2.TongTien = Integer.Parse(lbTongTien.Text)
        tmp2.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()

        Dim bus2 As HoaDonBUS = New HoaDonBUS()
        bus2.Update(tmp2)

        Dim bus As BanBUS = New BanBUS()
        bus.Update(tmp)

        gvBan_Load()
    End Sub
End Class
